function getHTTPObject() 
{ 
	var xmlhttp; 
	
	/*@cc_on 
	@if (@_jscript_version >= 5)
	{
		try 
		{ 
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
		} 
		catch (e) 
		{ 
			try 
			{ 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
			catch (E) 
			{ 
				xmlhttp = false; 
			} 
		} 
	}
	@else 
	{
		xmlhttp = false; 
	}
	@end @*/  
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{ 
		try 
		{ 
			xmlhttp = new XMLHttpRequest(); 			
		} 
		catch (e) 
		{ 
			xmlhttp = false; 
		} 
	} 
	
	return xmlhttp; 
} 

var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);
var xmlHttp = getHTTPObject();
	
function Refresh(clientID)
{	
	var mlxUserName = document.getElementById(clientID + 'mlxUserName');
	Send(clientID, "","R", mlxUserName.value);
}

function SendText(clientID)
{
	var mlxText = document.getElementById(clientID + 'mlxText');
	if(mlxText.value.length > 0)
	{
		SendMsg(clientID, mlxText.value);
		mlxText.value = '';
	}
}

function SendMsg(clientID, msg)
{
	var mlxUserName = document.getElementById(clientID + 'mlxUserName');
	Send(clientID, msg, 'M', mlxUserName.value);
}

function Join(clientID)
{
	var mlxUserName = document.getElementById(clientID + 'mlxUserName');
	Send(clientID, '', 'J', mlxUserName.value);
}

function Send(clientID, testo, operazione, userName) 
{
	xmlHttp.open('get', '/Chat/Request.ashx?id=' + clientID + '&t=' + testo + '&op=' + operazione + '&u=' + userName); 
	xmlHttp.onreadystatechange = handleResponse;
	xmlHttp.send(null); 	
} 

function KeyPress(e, clientID)
{ 
	var mlxBtn = document.getElementById(clientID + 'mlxBtn');
	if (typeof mlxBtn == 'object')
	{
		if(navigator.appName.indexOf("Netscape")>(-1))
			if (e.keyCode == 13)
			{
				mlxBtn.click();
				return false;
			}
		if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)) 
			if (event.keyCode == 13)
			{ 
				mlxBtn.click(); 
				return false; 
			} 	 
	}
} 

function handleResponse() 
{ 
	switch(xmlHttp.readyState) 
	{ 
		case 0: //uninitialized
			{ } 
			break; 
		case 1: //loading
			{ } 
			break; 
		case 2: //loaded
			{ } 
			break; 
		case 3: //interactive
			{ } 
			break; 
		case 4: //complete
			{ 
				if (xmlHttp.status == 200) 
				{
					var result = xmlHttp.responseText;
					var op = result.substring(0,1);
					result = result.substring(1);
					var clientID = result.substring(0, result.indexOf('\n'));
					result = result.substring(result.indexOf('\n') + 1);
					
					switch(op)
					{
						case "R":
						{
							setTimeout("Refresh(\'" + clientID + "\')", 5000);
							var mlxOut = document.getElementById(clientID + 'mlxOut');
							
							mlxOut.innerHTML = result;
							/*if(isIE)
								mlxOut.innerHTML = result;
							else
								mlxOut.textContent = result;*/
						}
						break;
						case "M":
						{
							var mlxOut = document.getElementById(clientID + 'mlxOut');
							//var mlxText = document.getElementById(clientID + 'mlxText');

							mlxOut.innerHTML = result;
							/*if(isIE)
								mlxOut.innerHTML = result;
							else
								mlxOut.textContent = result;*/
						}
						break;
					}
				} 
				else 
					alert("There was a problem retrieving the XML data:\n" + xmlHttp.statusText); 
			} 
			break; 
	}
}